/**
 * 功能说明：认证模块。
 */
import { loginApi, logoutApi } from '../../api/auth'

export default {
  namespaced: true,
  state: { token: null, user: null },
  getters: { isAuthenticated: s => !!s.token },
  mutations: {
    setAuth(state, { token, user }) { state.token = token; state.user = user },
    clearAuth(state) { state.token = null; state.user = null }
  },
  actions: {
    async login({ commit }, { username, password }) {
      const data = await loginApi({ username, password })
      commit('setAuth', { token: data.token, user: data.user })
      return data
    },
    async logout({ commit }) {
      await logoutApi(); commit('clearAuth')
    }
  }
}

